//
//  HWWebViewController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/14.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import WebKit

class HWWebViewController: HWBaseViewController {
    var urlString = ""
    var webView = WKWebView()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
    }
    

}

extension HWWebViewController {
    private func initUI() -> Void {
        
        navigationView.titleLabel.text = "云游戏"
        navigationView.backButton.setImage(UIImage(named: "navBackButton"), for: .normal)
        
        let configuration =    WKWebViewConfiguration()
        configuration.userContentController = WKUserContentController()
        configuration.allowsInlineMediaPlayback = true
        configuration.userContentController.add(self, name: "gameinfo")
        if #available(iOS 13.0, *) {
            configuration.defaultWebpagePreferences.preferredContentMode = .mobile
        } else {
            // Fallback on earlier versions
        }
        webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), configuration: configuration)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        
        let requesturl = URL(string: urlString) ?? URL(string: "https://www.baidu.com")
        let  request  =   NSMutableURLRequest(url: requesturl!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 60)
        webView.load(request as URLRequest)
        view.addSubview(webView)
        
    }
    
    private func initFrame() -> Void {
        webView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
    }
}
extension HWWebViewController :  WKUIDelegate,WKNavigationDelegate, WKScriptMessageHandler   {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {

        
    }
    
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
        print("成功")
        
        self.navigationItem.title =    webView.title
    }
    
    
    
    
    func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
        
    }
    
    
    
    override class func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  
        
    }
    
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
//        if(!(navigationAction.targetFrame?.isMainFrame ?? false)) {
//
//            webView.evaluateJavaScript("var a = document.getElementsByTagName('a');for(var i=0;i<a.length;i++){a[i].setAttribute('target','');}", completionHandler: nil)
//
//        }
//
        
        
        
        
        decisionHandler(.allow) ;
        
        
        
        
        
    }
    
    
    
    
    
    
}
